<template>
  <div class="container">
    <el-upload
      list-type="picture-card"
      action="#"
      :on-preview="preview"
      :file-list="fileList"
      :limit="1"
      :class="{ disabled: listComputed }"
      :on-remove="removeImage"
      :on-change="chageImage"
      :before-upload="updateImage"
      :http-request="upload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-progress v-if="showPercent" style="width:180px" :percentage="percent" />
    <el-dialog :visible.sync="showDialog">
      <img :src="imgURL" alt="" style="height:100%;width:100%">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDq6B0YS96WqgKrTK74hFI789uWqtA3YZU',
  SecretKey: 'pKcCgkb8YLuC31tZwZy82it5A4QZOuMm'
})
export default {
  data() {
    return {
      imgURL: '',
      showDialog: false,
      currentFileUid: null,
      percent: 0,
      showPercent: false,
      fileList: [
        {
          url:
            'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3068869606,3291850797&fm=26&gp=0.jpg'
        }
      ]
    }
  },
  computed: {
    listComputed() {
      return this.fileList.length === 1
    }
  },
  created() {},
  mounted() {},
  methods: {
    preview(file) {
      this.imgURL = file.url
      this.showDialog = true
    },
    removeImage(file, fileList) {
      this.fileList = fileList
    },
    chageImage(file, fileList) {
      this.fileList = fileList.map(item => item)
    },
    updateImage(file) {
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']

      if (!types.includes(file.type)) {
        this.$message.error('上传图片只能是 JPG、GIF、BMP、PNG 格式!')
        return false
      }
      const MaxSize = 5 * 1024 * 1024
      if (file.size > MaxSize) {
        this.$message.error('图片不得大于5M')
        return false
      }

      // file.uid
      this.currentFileUid = file.uid // 记住当前的uid
      this.showPercent = true
      return true
    },
    upload(params) {
      if (params.file) {
        cos.putObject(
          {
            Bucket: 'nzk-1305689099', // 桶名
            Region: 'ap-chongqing', // 桶所在地
            Key: params.file.name,
            Body: params.file,
            StorageClass: 'STANDARD',
            onProgress: params => {
              this.percent = params.percent * 100
            }
          },
          (err, data) => {
            console.log(err || data)
            if (!err && data.statusCode === 200) {
              this.fileList = this.fileList.map(item => {
                if (item.uid === this.currentFileUid) {
                  return { url: 'http://' + data.Location, upload: true }
                }
                return item
              })
              setTimeout(() => {
                this.showPercent = false
                this.percent = 0
              }, 1000)
            }
          }
        )
      }
    }
  }
}
</script>

<style>
.disabled .el-upload--picture-card {
  display: none;
}
</style>
